var myInfo = {};


(function($, doc) {
	$.init();
	$.ready(function() {
		/**
		 * 获取对象属性的值
		 * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
		 * @param {Object} obj 对象
		 * @param {String} param 属性名
		 */
		var _getParam = function(obj, param) {
			return obj[param] || '';
		};
		//普通示例
		var userPicker = new $.PopPicker();
		userPicker.setData([{
			value: '1',
			text: '一年级'
		}, {
			value: '2',
			text: '二年级'
		}, {
			value: '3',
			text: '三年级'
		}, {
			value: '4',
			text: '四年级'
		}, {
			value: '5',
			text: '五年级'
		}, {
			value: '6',
			text: '六年级'
		}, {
			value: '7',
			text: '初一'
		}, {
			value: '8',
			text: '初二'
		}, {
			value: '9',
			text: '初三'
		}]);
		var showUserPickerButton = doc.getElementById('showUserPicker');
		var userResult = doc.getElementById('gradeText');
		var grade = doc.getElementById('grade');
		showUserPickerButton.addEventListener('tap', function(event) {
			userPicker.show(function(items) {
//							userResult.innerText = JSON.stringify(items[0]);
				userResult.innerText = items[0].text;
				grade.value = items[0].value;
				//返回 false 可以阻止选择框的关闭
				//return false;
			});
		}, false);
		//------------家长关系选择框-----------------------------
        var relationPicker = new $.PopPicker();
        relationPicker.setData([{
            value: '母子',
            text: '母子'
        }, {
            value: '父子',
            text: '父子'
        }, {
            value: '其他',
            text: '其他'
        }]);
        var relationPickerButton = doc.getElementById('showRelationPicker');
        var userSelectResult = doc.getElementById('relationText');
        var parentsRelation = doc.getElementById('parentsRelation');
        relationPickerButton.addEventListener('tap', function(event) {
            relationPicker.show(function(items) {
                userSelectResult.innerText = items[0].text;
                parentsRelation.value = items[0].value;
                //返回 false 可以阻止选择框的关闭
                //return false;
            });
        }, false);
	});
})(mui, document);

//获取用户信息
myInfo.init = function(){	
	var options = {
		id:Feng.userId
	}	
	var ajax = new $ax(Feng.ctxPath+"/user/getById", function(data){        
        if (data.success){
        	for (var i in data.obj){
	            //判断Id与name是否相同
	            if($("#"+i).attr("name")==i){
	                $("#"+i).val(data.obj[i]);
	            }
	            
	            var grade = "";
	            switch(data.obj.grade){
	            	case "1":
	            		grade = "一年级";
	            		break;
	            	case "2":
	            		grade = "二年级";
	            		break;
	            	case "3":
	            		grade = "三年级";
	            		break;
	            	case "4":
	            		grade = "四年级";
	            		break;
	            	case "5":
	            		grade = "五年级";
	            		break;
	            	case "6":
	            		grade = "六年级";
	            		break;
	            	case "7":
	            		grade = "初一";
	            		break;
	            	case "8":
	            		grade = "初二";
	            		break;
	            	case "9":
	            		grade = "初三";
	            		break;
	            }
	            
	            $("#gradeText").text(grade);	            
	            
	            var sex = parseInt(data.obj.sex) - 1;
	            $("[name='sex']").eq(sex)[0].checked = true;

                $("#relationText").text(data.obj.parentsRelation);
	        }
        }
        else if(data.code === 2){
        	Feng.open(data.msg);
        }
        else{
        	Feng.log("获取用户信息失败");
        }        
   },function(data){    	
        Feng.log("获取用户信息失败");
    });
    ajax.set(options);
    ajax.start();	
}
//保存
myInfo.submitUser = function(){
	var userFrom = getFormJson("#frmMain");
	var ajax = new $ax(Feng.ctxPath+"/user/update", function(data){        
        if (data.success){
        	if(data.obj===null){
				Feng.success("保存成功");
        	}
        	else{
        		handSelCoupon.init(data);
        	}
            setTimeout(function(){
                window.location.href = "ui-me.html";
            },2000);
        }
        else if(data.code === 2){
        	Feng.error(data.msg);
        }
        else{
        	Feng.log("获取用户信息失败");
        }        
   },function(data){    	
        Feng.log("获取用户信息失败");
    });
    ajax.set(userFrom);
    ajax.start();
};

$("#btn-phone").on("click",function(){
	var phone = $("#phone").val();
	if(!phone){
		window.location.href = "bind-phone.html";
	}
	else{
		localStorage.setItem("phone",phone);		
		window.location.href = "update-phone.html";		
	}
});

//性别选择
$("[name='sex']").on("click",function(){	
	$("[name='sex']")[0].checked = false;
	$("[name='sex']")[1].checked = false;
	$(this)[0].checked = true;
});

$(function(){
	myInfo.init();
});
